package co.th.bjc.md.online.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
//import co.th.bjc.md.process.service.CallService;

public class InitialServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger _log = Logger.getLogger(getClass());
	private static final long LOG4J_DELAY = 300000;

	public InitialServlet() {
		super();
	}

	public void init() throws ServletException {
		super.init();
		String log4jinit = getServletContext().getRealPath("/log4j.properties");
		setupLogger(log4jinit);
		_log.info("initial log4j completed");

		//new CallService();
//		CallService.getInstance();
		_log.info("initial back end completed");
	}

	public static void setupLogger(String fileName) {
		try {
			boolean log4jIsConfigured = org.apache.log4j.Logger.getRoot().getAllAppenders().hasMoreElements();
			if (!log4jIsConfigured) {
				org.apache.log4j.PropertyConfigurator.configureAndWatch(fileName, LOG4J_DELAY);
			}

		} catch (Exception ex) {
			System.err.println("Unable to configure log4j from text file " + fileName);
			ex.printStackTrace();
		} 
		finally {
			org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(InitialServlet.class.getName());
			logger.info("Log4j Delay is " + LOG4J_DELAY);
			logger.info("Properties File is " + fileName);
		}
	}
}
